/*
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 *
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */

#include <unistd.h>
#include <errno.h>
#include <sys/io.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdlib.h>
#include <sys/time.h>
#include "net_zarubsys_unianalyzer_dataminers_lpt_internal_linux_provider_LinuxLptProvider.h"

JNIEXPORT jint JNICALL Java_net_zarubsys_unianalyzer_dataminers_lpt_internal_linux_provider_LinuxLptProvider_readByte (JNIEnv * env, jclass this, jint address) {
	return inb(address);
}

JNIEXPORT jintArray JNICALL Java_net_zarubsys_unianalyzer_dataminers_lpt_internal_linux_provider_LinuxLptProvider_readBytesWithDelay (JNIEnv * env, jclass this, jint address, jint count, jint delay) {
	jintArray javaArray = (*env)->NewIntArray(env, count);
	jint *nativeElements = (*env)->GetIntArrayElements(env, javaArray, NULL);
	int i;
	for (i = 0; i < count; i++) {
		nativeElements[i] = inb(address);
		// delay will be here
	}
	(*env)->ReleaseIntArrayElements(env, javaArray, nativeElements, 0);
	return javaArray;
}

JNIEXPORT jintArray JNICALL Java_net_zarubsys_unianalyzer_dataminers_lpt_internal_linux_provider_LinuxLptProvider_readBytesWithDelayAndTrigger
  (JNIEnv * env, jclass this, jint address, jint count, jint delay, jint triggerMask, jint firstResult, jint secondResult, jlong maxWaitingInterval) {

	struct timeval beginTime;
	struct timeval currentTime;

	jintArray javaArray = (*env)->NewIntArray(env, count);
	jint *nativeElements = (*env)->GetIntArrayElements(env, javaArray, NULL);

	gettimeofday(&beginTime, NULL);

	int wait = 1;
	int firstWait = 1;
	while (wait) {
		int state = inb(address);
		int resultToMatch = (firstWait ? firstResult : secondResult);
		if ((state & triggerMask) == resultToMatch) {
			if (firstWait) {
				firstWait = 0;
			} else {
				wait = 0;
			}
		} else {

			gettimeofday(&currentTime, NULL);
			long time = ((currentTime.tv_sec - beginTime.tv_sec) * 1000 * 1000 + labs(currentTime.tv_usec - beginTime.tv_usec)) * 1000;
			if (time >= maxWaitingInterval) {
				wait = 0;
			}

		}
	}


	int i;
	for (i = 0; i < count; i++) {
		nativeElements[i] = inb(address);
		// delay will be here
	}

	(*env)->ReleaseIntArrayElements(env, javaArray, nativeElements, 0);
	return javaArray;

}

JNIEXPORT jint JNICALL Java_net_zarubsys_unianalyzer_dataminers_lpt_internal_linux_provider_LinuxLptProvider_openPort(JNIEnv * env, jclass this, jint address) {
	int fileDescriptor = open()
}

JNIEXPORT jint JNICALL Java_net_zarubsys_unianalyzer_dataminers_lpt_internal_linux_provider_LinuxLptProvider_closePort(JNIEnv * env, jclass this, jint address) {
	if (ioperm(address, 3, 0) < 0) {
		return errno;
	}
  	return 0;
}
